package cn.stylefeng.guns.generator.executor.config;

import cn.stylefeng.guns.generator.engine.base.GunsTemplateEngine;
import cn.stylefeng.guns.generator.engine.config.ContextConfig;
import cn.stylefeng.guns.generator.engine.config.SqlConfig;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import lombok.Data;

import java.util.List;

@Data
public class GunsGeneratorConfig{

    GlobalConfig globalConfig = new GlobalConfig();
    DataSourceConfig dataSourceConfig = new DataSourceConfig();
    StrategyConfig strategyConfig = new StrategyConfig();
    PackageConfig packageConfig = new PackageConfig();
    TableInfo tableInfo = null;
    ContextConfig contextConfig ;
    SqlConfig sqlConfig;

    public void init(String projectPath,String bizEnName,String bizChName,String moduleName,String proPackage,String tableNames,String parentMenuName){
        contextConfig = new ContextConfig(projectPath,bizEnName,moduleName);
        contextConfig.setProPackage(proPackage);
        contextConfig.setCoreBasePackage(proPackage);
        contextConfig.setBizChName(bizChName);
        globalConfig.setOutputDir(projectPath+"\\src\\main\\java");

        packageConfig.setParent(null);
        config();
        packageConfig.setServiceImpl(contextConfig.getServicePackage());
        packageConfig.setEntity(contextConfig.getEntityPackage());
        packageConfig.setMapper(contextConfig.getMapperPackage());
        strategyConfig.setSuperMapperClass("com.baomidou.mybatisplus.core.mapper.BaseMapper");
        strategyConfig.setInclude(tableNames);

        sqlConfig = new SqlConfig(parentMenuName,contextConfig);
    }

    protected void config() {
        globalConfig();
        dataSourceConfig();
        strategyConfig();
    }

    protected void globalConfig() {
        globalConfig.setFileOverride(true);
        globalConfig.setEnableCache(false);
        globalConfig.setBaseResultMap(true);
        globalConfig.setBaseColumnList(true);
        globalConfig.setOpen(false);
        globalConfig.setAuthor("wanjian");
        globalConfig.setServiceImplName("%sService");
        globalConfig.setSwagger2(true);
        globalConfig.setDateType(DateType.ONLY_DATE);
    }

    protected void dataSourceConfig() {
        dataSourceConfig.setDbType(DbType.MYSQL);
        dataSourceConfig.setDriverName("com.mysql.jdbc.Driver");
        dataSourceConfig.setUsername("root");
        dataSourceConfig.setPassword("sunwell");
        dataSourceConfig.setUrl("jdbc:mysql://127.0.0.1:3306/guns?characterEncoding=utf8");
    }

    protected void strategyConfig() {
        strategyConfig.setTablePrefix(new String[]{"sys_"});// 此处可以修改为您的表前缀
        strategyConfig.setNaming(NamingStrategy.underline_to_camel);
    }


    public StrategyConfig getStrategyConfig() {
        return strategyConfig;
    }


    public void doMpGeneration() {
        AutoGenerator autoGenerator = new AutoGenerator();
        autoGenerator.setGlobalConfig(globalConfig);
        autoGenerator.setDataSource(dataSourceConfig);
        autoGenerator.setStrategy(strategyConfig);
        autoGenerator.setPackageInfo(packageConfig);
        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setServiceImpl("gunsTemplate/advanced/serviceImpl.java.vm");
        templateConfig.setService(null);
        templateConfig.setXml(null);
        templateConfig.setController(null);
        autoGenerator.setTemplate(templateConfig);
        autoGenerator.execute();

        //获取table信息,用于guns代码生成
        List<TableInfo> tableInfoList = autoGenerator.getConfig().getTableInfoList();
        if (tableInfoList != null && tableInfoList.size() > 0) {
            this.tableInfo = tableInfoList.get(0);
        }
    }

    public void doGunsGeneration(GunsGeneratorConfig gunsGeneratorConfig) {
        gunsGeneratorConfig.setPackageConfig(packageConfig);
        GunsTemplateEngine gunsTemplateEngine = new GunsTemplateEngine(gunsGeneratorConfig);
        sqlConfig.setConnection(dataSourceConfig.getConn());
        gunsTemplateEngine.start(packageConfig);
    }

}
